package com.zmm.weblog.web.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmm.weblog.common.domain.dos.ArticleDo;
import com.zmm.weblog.common.domain.dos.ArticleTagRelDo;
import com.zmm.weblog.common.domain.dos.TagDo;
import com.zmm.weblog.common.domain.mapper.*;
import com.zmm.weblog.common.enums.ResponseCodeEnum;
import com.zmm.weblog.common.exception.BizException;
import com.zmm.weblog.common.utils.PageResponse;
import com.zmm.weblog.common.utils.Response;
import com.zmm.weblog.web.convert.ArticleConvert;
import com.zmm.weblog.web.convert.TagConvert;
import com.zmm.weblog.web.module.vo.tag.FindTagArticlePageListReqVo;
import com.zmm.weblog.web.module.vo.tag.FindTagArticlePageListRspVo;
import com.zmm.weblog.web.module.vo.tag.FindTagListRspVo;
import com.zmm.weblog.web.service.TagService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 福禄寿
 * @Date: 2024/7/8 下午4:55
 * @Des: weblog-springboot
 **/
@Service
public class TagServiceImpl implements TagService {


    private static final Logger log = LoggerFactory.getLogger(TagServiceImpl.class);
    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private ArticleTagRelMapper articleTagRelMapper;

    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public Response findTagList() {
        //获取分类信息
        List<TagDo> tagDos = tagMapper.selectList(null);
        List<FindTagListRspVo> vos = null;
        if (!CollectionUtils.isEmpty(tagDos)){
            //DO转VO
            vos = tagDos.stream()
                    .map(TagConvert.INSTANCE::convertDO2VO).collect(Collectors.toList());
        }
        //获取标签信息
        return Response.success(vos);
    }

    @Override
    public Response findTagPageList(FindTagArticlePageListReqVo findTagArticlePageListReqVo) {
        Long current = findTagArticlePageListReqVo.getCurrent();
        Long size = findTagArticlePageListReqVo.getSize();
        Long tagId = findTagArticlePageListReqVo.getId();

        TagDo tagDo = tagMapper.selectById(tagId);
        if (tagDo == null){
            log.info("标签不存在,id:{}",tagId);
            throw new BizException(ResponseCodeEnum.TAG_NOT_EXISTED);
        }
        List<ArticleTagRelDo> articleTagRelDos = articleTagRelMapper.selectListByTagId(tagId);

        if (articleTagRelDos == null){
            log.info("标签关联文章不存在,id:{}",tagId);
            throw new BizException(ResponseCodeEnum.ARTICLE_NOT_FOUND);
        }
        List<Long> articleIds = articleTagRelDos.stream().map(ArticleTagRelDo::getArticleId).collect(Collectors.toList());
        Page<ArticleDo> page = articleMapper.selectPageListByArticleIds(current,size,articleIds);
        List<ArticleDo> articleDos = page.getRecords();
        List<FindTagArticlePageListRspVo> vos = null;
        if (!CollectionUtils.isEmpty(articleDos)){
            vos = articleDos.stream()
                    .map(ArticleConvert.INSTANCE::convertDO2TagArticleVO).collect(Collectors.toList());
        }
        return PageResponse.success(page,vos);
    }
}
